extends Node

#成长类信息传递
signal growth(plus:EquipItem,item:EquipItem)
#属性总加成
signal current(item:EquipItem)




var source_list :Array[EquipItemGroup] = []

var a = preload("res://entities/equip_item/group/EquipGroup1.tres")

var equip_info_map: Dictionary[String, EquipItem] = {}


func _ready() -> void:
	var a = ResourceLoader.load("res://entities/equip_item/group/EquipGroup1.tres") as EquipItemGroup
	source_list.append(a) 
	
	for item:EquipItem in a.equip_infi_list:
		equip_info_map[item.character_name] = item
	

func get_equip(name:String) -> EquipItem:
	return equip_info_map.get(name).duplicate()


#获取某个等级的足额数量
func get_level_equip(index :int ,amount:int) -> Array:
	
	if index >=source_list.size():
		return[]
	
	
	var result :Array = []
	var size = len(source_list[index].equip_infi_list)
	
	for i in range(amount):
		result.append(source_list[index].equip_infi_list[randi()%size].duplicate())
	
	return result
	

#获取从1到某个等级的足额数量
func get_to_level_equip(level :int ,amount:int) -> Array:
	var result :Array = []
	var avg_count = amount/level
	var left = amount%level
	
	for i in range(level):
		#if i < level-1:
		result.append_array(get_level_equip(i,avg_count))
		#else:
			#result.append(get_level_equip(i+1,avg_count + left))
	return result


#计算这些装备对角色的总加成
func get_caculate(equip_item_list:Array[EquipItem]) -> EquipItem:
	var result = EquipItem.new()
	
	for equip_item in equip_item_list:
		result.attack_damage += equip_item.attack_damage
		result.magic_damage += equip_item.magic_damage
		result.health += equip_item.health
		result.magic += equip_item.magic
		result.attack_defult += equip_item.attack_defult
		result.magic_defult += equip_item.magic_defult
		
		result.attack_damage_rate += equip_item.attack_damage_rate
		result.magic_damage_rate += equip_item.magic_damage_rate
		result.health_rate += equip_item.health_rate
		result.magic_rate += equip_item.magic_rate
		result.attack_defult_rate += equip_item.attack_defult_rate
		result.magic_defult_rate += equip_item.magic_defult_rate
		
	
	return result


#计算这些装备对角色的总结算加成
func get_caculate_growth(equip_item_list:Array[EquipItem]) -> EquipItem:
	
	var result = EquipItem.new()
	var jiesuan_count = 1
	var jiesuan_plus = 1
	for equip_item in equip_item_list:
		if equip_item.type == EquipItem.Type.结算:
			jiesuan_count += 1
			result.attack_damage_growth += equip_item.attack_damage_growth
			result.magic_damage_growth  += equip_item.attack_damage_growth
			result.health_growth  += equip_item.attack_damage_growth
			result.magic_growth += equip_item.attack_damage_growth
			result.attack_defult_growth += equip_item.attack_damage_growth
			result.magic_defult_growth += equip_item.attack_damage_growth
			result.rate_growth += equip_item.rate_growth
		
	return result


#消耗装备后对装备的加成
func get_caculate_destory_growth(equip_item_context:EquipItem , equip_item_list:Array[EquipItem]) -> EquipItem:
	
	#不是破坏类的物品，无法被破坏类的物品继承属性
	if equip_item_context.type != EquipItem.Type.破坏:
		return equip_item_context
	
	var result = equip_item_context.duplicate() as EquipItem
	
	#被破坏的装备时破坏成长型的，那么这里进行结算
	if result.type_growth == EquipItem.TypeGrowth.成长叠加:
		result.attack_damage += result.attack_damage_growth * result.rate_growth
		result.magic_damage += result.magic_damage_growth * result.rate_growth
		result.health += result.health_growth * result.rate_growth
		result.magic += result.magic_growth * result.rate_growth
		result.attack_defult += result.attack_defult_growth * result.rate_growth
		result.magic_defult += result.magic_defult_growth * result.rate_growth
	
	
	
	
	#已经有破坏类的吸收属性了
	var destroied = false
	for equip_item in equip_item_list:
		if equip_item.type == EquipItem.Type.破坏 and equip_item.type_growth == EquipItem.TypeGrowth.普通叠加 and !destroied:
			equip_item.attack_damage += result.attack_damage
			equip_item.magic_damage += result.magic_damage
			equip_item.health += result.health
			equip_item.magic += result.magic
			equip_item.attack_defult += result.attack_defult
			equip_item.magic_defult += result.magic_defult
			
			equip_item.attack_damage_rate += result.attack_damage_rate
			equip_item.magic_damage_rate += result.magic_damage_rate
			equip_item.health_rate += result.health_rate
			equip_item.magic_rate += result.magic_rate
			equip_item.attack_defult_rate += result.attack_defult_rate
			equip_item.magic_defult_rate += result.magic_defult_rate
			destroied = true
		
		if equip_item.type == EquipItem.Type.破坏 and equip_item.type_growth == EquipItem.TypeGrowth.成长叠加:
			equip_item.attack_damage_growth += equip_item.attack_damage
			equip_item.magic_damage_growth  += equip_item.attack_damage
			equip_item.health_growth  += equip_item.attack_damage
			equip_item.magic_growth += equip_item.attack_damage
			equip_item.attack_defult_growth += equip_item.attack_damage
			equip_item.magic_defult_growth += equip_item.attack_damage
			
			
		
		
			
	return result
